import axios from "axios";
import {ElMessage} from "element-plus";
export {post, get}
// 定义默认的 failure 函数
function defaultFailure(message) {
    ElMessage.warning(message);
}

// 定义默认的 error 函数
function defaultError() {
    ElMessage.error("网络请求失败，请检查网络连接");
}

function post(url, data, success, failure = defaultFailure, error = defaultError) {
    axios.post(url, data, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        withCredentials: true // 允许跨域携带cookie
    }).then(({data}) => {
        if (data.success)
            success(data.message, data.status);
        else
            failure(data.message, data.status);
    }).catch(error);
}
function get(url, success, failure = defaultFailure, error = defaultError) {
    axios.get(url, {
        withCredentials: true // 允许跨域携带cookie
    }).then(({data}) => {
        if (data.success)
            success(data.message, data.status);
        else
            failure(data.message, data.status);
    }).catch(error);
}